Scala has similar conditional expressions as available in other programming languages.
1. If Statement
The statement inside the if block will only execute when the expression is true.
Syntax:
Note : In Scala, adding a semicolon(;) at the end of statements is optional and generally avoided, unlike Java .
Example :
Output :
2. If else Statement
In the if-else statement, either if or else will execute based on the result of the expression. If the expression is true, then the statement inside the if block will execute; otherwise, statements inside the else block will execute.
Syntax :
Example :
Output :
3. If else ladder
Syntax:
Example:
Output:
4. Nested If else
This expression is used when one if-else is needed inside another if-else block. It is said to be nested if-else.
Example:
Output:
Another example:
Output: